#ifndef LINUX_CONTROLLER_H
#define LINUX_CONTROLLER_H

#include <QString>
#include <QFile>
#include <QLabel>
#include <QMessageBox>
#include <QDir>
#include <QApplication>
#include <QPushButton>
#include <QSysInfo>
#include <QTableWidget>
#include <QRegularExpression>
#include <QtConcurrent/QtConcurrent>

class odbc_ini
{
public:
    QString odbc_ini_location;
    QString odbc_source;
    QString ip;
    QString port;
    QString uid;
    QString pwd;
    bool file_status;

    odbc_ini(QString location = "/usr/local/etc/odbc.ini")
    {
        this -> odbc_ini_location = location;
        QFile odbc(this -> odbc_ini_location);
        QString ini_index;
        if(odbc.open(QIODevice::ReadOnly))
        {
            ini_index = odbc.readAll();
            this -> odbc_source = ini_index.split(']')[0].split('[')[1].remove(' ');
            this -> ip = ini_index.split("Servername=")[1].split("\nDatabase")[0].remove(' ');
            this -> port = ini_index.split("Port=")[1].split("\nSslmode")[0].remove(' ');
            this -> uid = ini_index.split("Username=")[1].split("\nPassword")[0].remove(' ');
            this -> pwd = ini_index.split("Password=")[1].split("\nPort")[0].remove(' ');
            file_status = true;
        }
        else
        {
            QMessageBox mbox;
            mbox.setText("读取odbc.ini错误");
            mbox.exec();
            file_status = false;
        }
        odbc.close();
    }

    ~odbc_ini()
    {

    }
};

void FreshNodeList(QTableWidget* NodeTable, QTableWidget* NodeDetailTable)
{
    QFile txt(QApplication::applicationDirPath() + "/NodeList.ini");
    if(txt.exists())
    {
        QString cmd2 = "rm \'" + QApplication::applicationDirPath() + "/NodeList.ini\'";
        system(cmd2.toLatin1().data());
    }

    NodeTable -> clearContents();
    NodeTable -> setRowCount(0);

    NodeDetailTable -> clearContents();
    NodeDetailTable -> setRowCount(0);

    QString cmd = "gs_om -t status --detail >> \'" + QApplication::applicationDirPath() + "/NodeList.ini\'";

    QByteArray cmdbyte = cmd.toLatin1();

    system(cmdbyte.data());

    if(txt.open(QIODevice::ReadOnly))
    {
        QString TxtIndex = txt.readAll();
        QStringList TempList = TxtIndex.split('\n');

        bool ReadingNodeIndex = false;
        for(int i = 0; i < TempList.size(); i++)
        {
            if(ReadingNodeIndex)
            {
                QRegularExpression Blanks("[\\s]+");

                TempList[i].replace(Blanks, " ");
                QStringList Temp = TempList[i].split(' ');

                if(Temp.size() >= 4)
                {
                    NodeTable -> insertRow(NodeTable -> rowCount());
                    NodeTable -> setItem(NodeTable -> rowCount() - 1, 0, new QTableWidgetItem(Temp[1]));
                    NodeTable -> setItem(NodeTable -> rowCount() - 1, 1, new QTableWidgetItem(Temp[2]));
                    NodeTable -> setItem(NodeTable -> rowCount() - 1, 2, new QTableWidgetItem(Temp[3]));

                    if(Temp.contains("Normal") || Temp.contains("normal"))
                    {
                        NodeTable -> setItem(NodeTable -> rowCount() - 1, 3, new QTableWidgetItem("已启动"));
                    }
                    else if(Temp.contains("Stopped") || Temp.contains("stopped"))
                    {
                        NodeTable -> setItem(NodeTable -> rowCount() - 1, 3, new QTableWidgetItem("离线"));
                    }
                    else
                    {
                        NodeTable -> setItem(NodeTable -> rowCount() - 1, 3, new QTableWidgetItem("不可用"));
                    }

                    if(Temp.contains("Primary"))
                    {
                        NodeTable -> setItem(NodeTable -> rowCount() - 1, 4, new QTableWidgetItem("主实例"));
                    }
                    else if(Temp.contains("Standby"))
                    {
                        NodeTable -> setItem(NodeTable -> rowCount() - 1, 4, new QTableWidgetItem("备实例"));
                    }
                    else if(Temp.contains("Pending"))
                    {
                        NodeTable -> setItem(NodeTable -> rowCount() - 1, 4, new QTableWidgetItem("仲裁"));
                    }
                    else if(Temp.contains("Down"))
                    {
                        NodeTable -> setItem(NodeTable -> rowCount() - 1, 4, new QTableWidgetItem("宕机"));
                    }
                    else
                    {
                        NodeTable -> setItem(NodeTable -> rowCount() - 1, 4, new QTableWidgetItem("未知"));
                    }

                    NodeDetailTable -> insertRow(NodeDetailTable -> rowCount());
                    NodeDetailTable -> setItem(NodeDetailTable -> rowCount() - 1, 0, new QTableWidgetItem(Temp[1]));
                    NodeDetailTable -> setItem(NodeDetailTable -> rowCount() - 1, 1, new QTableWidgetItem(Temp[2]));
                    NodeDetailTable -> setItem(NodeDetailTable -> rowCount() - 1, 2, new QTableWidgetItem(Temp[3]));

                    if(Temp.contains("Normal") || Temp.contains("normal"))
                    {
                        NodeDetailTable -> setItem(NodeDetailTable -> rowCount() - 1, 3, new QTableWidgetItem("已启动"));
                    }
                    else if(Temp.contains("Stopped") || Temp.contains("stopped"))
                    {
                        NodeDetailTable -> setItem(NodeDetailTable -> rowCount() - 1, 3, new QTableWidgetItem("离线"));
                    }
                    else
                    {
                        NodeDetailTable -> setItem(NodeDetailTable -> rowCount() - 1, 3, new QTableWidgetItem("不可用"));
                    }

                    if(Temp.contains("Primary"))
                    {
                        NodeDetailTable -> setItem(NodeDetailTable -> rowCount() - 1, 4, new QTableWidgetItem("主实例"));
                    }
                    else if(Temp.contains("Standby"))
                    {
                        NodeDetailTable -> setItem(NodeDetailTable -> rowCount() - 1, 4, new QTableWidgetItem("备实例"));
                    }
                    else if(Temp.contains("Pending"))
                    {
                        NodeDetailTable -> setItem(NodeDetailTable -> rowCount() - 1, 4, new QTableWidgetItem("仲裁"));
                    }
                    else if(Temp.contains("Down"))
                    {
                        NodeDetailTable -> setItem(NodeDetailTable -> rowCount() - 1, 4, new QTableWidgetItem("宕机"));
                    }
                    else
                    {
                        NodeDetailTable -> setItem(NodeDetailTable -> rowCount() - 1, 4, new QTableWidgetItem("未知"));
                    }
                }
            }

            if(TempList[i].contains("-----"))
            {
                ReadingNodeIndex = true;
            }
        }
    }
    else
    {

    }

    txt.close();
    QString cmd2 = "rm \'" + QApplication::applicationDirPath() + "/NodeList.ini\'";
    system(cmd2.toLatin1().data());
}

void get_db_status(QLabel* notice, QPushButton* button = nullptr, QTableWidget* NodeTable = nullptr, QTableWidget* NodeDetailTable = nullptr)
{
    QString result_qs = QApplication::applicationDirPath() + "/gbase_status.txt";
    QString cmd1 = "rm \'" + result_qs + "\'";
    QByteArray cmd1b = cmd1.toLatin1();
    QString cmd2 = "gs_om -t status >> \'" + result_qs + "\'";
    QByteArray cmd2b = cmd2.toLatin1();
    system(cmd2b.data());
    QFile txt(result_qs);
    if(txt.open(QIODevice::ReadOnly))
    {
        QString index = txt.readAll();
        if(index.contains("Normal"))
        {
            notice -> setText("已开启");
            if(button != nullptr)
            {
                button -> setText("关闭数据库");
                button -> setEnabled(true);
            }
        }
        else
        {
            notice -> setText("未开启");
            if(button != nullptr)
            {
                button -> setText("开启数据库");
                button -> setEnabled(true);
            }
        }
    }
    else
    {
        notice->setText("获取失败");
        if(button != nullptr)
        {
            button -> setText("重试");
            button -> setEnabled(true);
        }
    }
    system(cmd1b.data());
    FreshNodeList(NodeTable, NodeDetailTable);
}

void control_db(QString command, QLabel* notice, QPushButton* button = nullptr, QTableWidget* NodeList = nullptr, QTableWidget* NodeDetailTable = nullptr)
{
    if(!command.isEmpty())
    {
        system(command.toUtf8());
    }

    get_db_status(notice, button, NodeList, NodeDetailTable);
}

class LinuxController
{
public:
    static void RunCommand(QString Command)
    {
        if(!Command.isEmpty())
        {
            QtConcurrent::run(system, Command.toUtf8());
        }
    }

    static void FreshNodeList(QTableWidget* NodeTable, QTableWidget* NodeDetailTable)
    {
        QtConcurrent::run(FreshNodeList, NodeTable, NodeDetailTable);
    }
};

#endif // LINUX_CONTROLLER_H
